from datetime import date, datetime
from typing import Union

from fastapi import Query
from pydantic import BaseModel, Field

from forward.common.enums import LoginClientEnum
from forward.schema_base import EmptyStrToNone
from forward.utils.tools import ToolsUtil


class UserListInSchema(BaseModel):
    """用户列表参数"""

    keyword: str = Field(default="")  # 搜索关键词
    channel: Union[LoginClientEnum, None, EmptyStrToNone] = Field(
        default=""
    )  # 客户端类型: [1=微信小程序, 2=微信公众号, 3=手机H5；4=电脑PC, 5=苹果APP, 6=安卓APP]
    start_time: Union[date, datetime, None, EmptyStrToNone] = Query(None)  # 开始时间
    end_time: Union[date, datetime, None, EmptyStrToNone] = Query(None)  # 结束时间


class UserDetailInSchema(BaseModel):
    """用户列表参数"""

    id: int = Query(gt=0)


class UserEditlInSchema(BaseModel):
    id: int = Query(gt=0)
    field: str
    value: str


class UserInfoOutSchema(BaseModel):
    """
    用户信息输出
    """

    id: int  # 主键
    sn: int
    avatar: str
    real_name: str = Field()
    nickname: str
    username: str
    mobile: str
    sex: str | int
    channel: str | int
    last_login_ip: str = Field()
    last_login_time: datetime | None = Field(None)  # 最后登录时间
    created_time: datetime | None = Field(None)  # 创建时间
    model_config = {
        "alias_generator": ToolsUtil.to_lower_camel,
        "populate_by_name": True,
    }


class UserSchema(BaseModel):
    """
    用户信息输出
    """

    id: int  # 主键
    sn: int
    avatar: str
    real_name: str = Field()
    nickname: str
    username: str
    mobile: str
    sex: int
    channel: int
    last_login_ip: str = Field()
    last_login_time: datetime | None = Field(None)  # 最后登录时间
    created_time: datetime | None = Field(None)  # 创建时间
